package com.zuoqi.easywork.projectManage.service;

import com.zuoqi.common.utils.IMap;
import com.zuoqi.easywork.moneyManage.mapper.TcFundMapper;
import com.zuoqi.easywork.moneyManage.mapper.TcFundProjectItemMapper;
import com.zuoqi.easywork.moneyManage.mapper.TcFundUptItemMapper;
import com.zuoqi.easywork.moneyManage.model.TcFund;
import com.zuoqi.easywork.moneyManage.model.TcFundProjectItem;
import com.zuoqi.easywork.moneyManage.model.TcFundUptItem;
import com.zuoqi.easywork.projectManage.mapper.TcProjectFundMapper;
import com.zuoqi.easywork.projectManage.mapper.TcProjectMapper;
import com.zuoqi.easywork.projectManage.mapper.TcProjectUserMapper;
import com.zuoqi.easywork.projectManage.model.TcProject;
import com.zuoqi.easywork.projectManage.model.TcProjectFund;
import com.zuoqi.easywork.projectManage.model.TcProjectUser;
import com.zuoqi.easywork.system.service.OpLogService;
import com.zuoqi.easywork.userManage.mapper.TcUserScheduleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ProjectService {
    @Autowired
    private TcProjectMapper tcProjectMapper;
    @Autowired
    private OpLogService opLogService;
    @Autowired
    private TcProjectFundMapper tcProjectFundMapper;
    @Autowired
    private TcFundProjectItemMapper tcFundProjectItemMapper;
    @Autowired
    private TcFundMapper tcFundMapper;
    @Autowired
    private TcFundUptItemMapper tcFundUptItemMapper;
    @Autowired
    private TcProjectUserMapper tcProjectUserMapper;
    /**
     *获取项目列表（管理员与执行经理共用）
     */
    public List<IMap> getProjectListForManage(IMap params){
        return tcProjectMapper.getProjectListForManage(params);
    }
    /**
     * 添加项目信息
     * */
    public int addProject(IMap params){
        TcProject tcProject = new TcProject();
        SimpleDateFormat tempDate = new SimpleDateFormat("yyyyMMddHHmmss");
        String datetime = tempDate.format(new Date());
        tcProject.setProId("p"+datetime);
        tcProject.setProName(params.getString("proName"));
        //Sting -> Date
        DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
        ParsePosition pos = new ParsePosition(0);
        tcProject.setPredictEndTime(format1.parse(params.getString("predictEndTime"),pos));
        tcProject.setiDepart(params.getString("iDepart"));
        tcProject.setState("0");
        tcProject.setProDescribe(params.getString("proDescribe"));
        try {
            tcProjectMapper.insert(tcProject);
            opLogService.addLog(params.getString("updateUser"),"添加项目","添加项目ID："+tcProject.getProId());
        }catch (Exception e){
            return 0;
        }
        return 1;
    }
    /**
     * 修改项目信息
     * */
    public int UpdProject(IMap params){
        TcProject tcProject = new TcProject();
        tcProject.setProId(params.getString("proId"));
        tcProject.setProName(params.getString("proName"));
        //Sting -> Date
        DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
        ParsePosition pos = new ParsePosition(0);
        tcProject.setPredictEndTime(format1.parse(params.getString("predictEndTime"),pos));
        tcProject.setiDepart(params.getString("iDepart"));
        tcProject.setState("0");
        tcProject.setProDescribe(params.getString("proDescribe"));
        try {
            tcProjectMapper.updateByPrimaryKeySelective(tcProject);
            opLogService.addLog(params.getString("updateUser"),"修改项目信息","修改项目ID："+tcProject.getProId());
        }catch (Exception e){
            return 0;
        }
        return 1;
    }
    /**
     * 启动项目
     * */
    public int doStart(IMap params){
        TcProject tcProject = new TcProject();
        String proId = params.getString("proId");
        tcProject.setProId(proId);
        tcProject.setStartTime(new Date());
        tcProject.setState("1");
        try {
            tcProjectMapper.updateByPrimaryKeySelective(tcProject);
            opLogService.addLog(params.getString("updateUser"),"项目启动","启动项目ID："+proId);
        }catch (Exception e){
            return 0;
        }
        return 1;
    }
    /**
     * 获取项目详细信息
     * */
    public IMap getProjactInfo(String proId){
        return tcProjectMapper.getProjactInfo(proId);
    }
    /**
     * 废止项目
     * */
    public int doEnd(IMap params){
        TcProject tcProject = new TcProject();
        String proId = params.getString("proId");
        tcProject.setProId(proId);
        tcProject.setEndTime(new Date());
        tcProject.setState("9");
        try {
            tcProjectMapper.updateByPrimaryKeySelective(tcProject);
            opLogService.addLog(params.getString("updateUser"),"项目作废","废止项目ID："+proId);
        }catch (Exception e){
            return 0;
        }
        return 1;
    }
    /**
     * 核查公司账目是否满足预分配项目配额
     * */
    public int fundCheck(String money){
        IMap result =tcFundMapper.getFundInfo();
        int addMoney = Double.valueOf(money).intValue();
        if (result.getInt("money") > addMoney){
            return 1;
        }else {
            return 0;
        }
    }
    /**
     * 增加项目配额
     * */
    public int addProjectFund (IMap param){
        String proId = param.getString("proId");
        int addMoney = Double.valueOf(param.getString("addMoney")).intValue();
        TcProjectFund result = tcProjectFundMapper.selectByPrimaryKey(proId);
        //插入项目资金表
        TcProjectFund tcProjectFund = new TcProjectFund();
        tcProjectFund.setProId(proId);
        //插入资金变动表
        TcFundUptItem tcFundUptItem = new TcFundUptItem();
        SimpleDateFormat tempDate = new SimpleDateFormat("yyyyMMddHHmmss");
        String datetime = tempDate.format(new Date());
        tcFundUptItem.setItemId("u"+datetime);
        tcFundUptItem.setItemName("项目分配");
        tcFundUptItem.setMoney(addMoney);
        tcFundUptItem.setOperation("2");
        tcFundUptItem.setOpTime(new Date());
        //修改账户金额
        IMap result1 =tcFundMapper.getFundInfo();
        int newMoney = result1.getInt("money") - addMoney;
        TcFund tcFund =new TcFund();
        tcFund.setFundId(result1.getString("fund_id"));
        tcFund.setMoney(newMoney);
        //插入资金变动对应项目表
        TcFundProjectItem tcFundProjectItem = new TcFundProjectItem();
        tcFundProjectItem.setProId(proId);
        tcFundProjectItem.setFundItemId(tcFundUptItem.getItemId());
        try {
            if (result == null){
                tcProjectFund.setMoney(addMoney);
                tcProjectFund.setMoneymax(addMoney);
                tcProjectFund.setState("1");
                tcProjectFundMapper.insert(tcProjectFund);
            }else {
                tcProjectFund.setMoney(addMoney + result.getMoney());
                tcProjectFund.setMoneymax(addMoney + result.getMoneymax());
                tcProjectFundMapper.updateByPrimaryKeySelective(tcProjectFund);
            }
            tcFundUptItemMapper.insert(tcFundUptItem);
            tcFundMapper.updateByPrimaryKeySelective(tcFund);
            tcFundProjectItemMapper.insert(tcFundProjectItem);
            opLogService.addLog(param.getString("updateUser"),"增加项目配额","增加项目ID："+proId);
        }catch (Exception e){
            return 0;
        }
        return 1;
    }

    /**
     * 获取当前部门部员
     * */
    public  List<IMap> selectDepartUser(IMap param) {
        return  tcProjectUserMapper.selectDepartUser(param);
    }
    /**
     * 分配项目人员
     * */
    public int addProjectUser(IMap param){
        String proId = param.getString("proId");
        tcProjectUserMapper.deleteByProId(proId);
        if (param.getString("userIds").length()==0)return 0;
        String[] ids =param.getString("userIds").split(",");
        for (String userId : ids){
            TcProjectUser tcProjectUser = new TcProjectUser();
            tcProjectUser.setProId(proId);
            tcProjectUser.setUserId(userId);
            tcProjectUser.setState("1");
            tcProjectUserMapper.insert(tcProjectUser);
        }
        opLogService.addLog(param.getString("updateUser"),"分配项目人员","项目ID："+proId);
        return 1;
    }
    /**
     * 确定结束项目
     * */
    public int doEndProject(IMap param){
        String proId = param.getString("proId");
        //项目结束标记
        TcProject tcProject = new TcProject();
        tcProject.setProId(proId);
        tcProject.setEndTime(new Date());
        tcProject.setState("2");
        //获取剩余配额
        TcProjectFund tcProjectFund = tcProjectFundMapper.selectByPrimaryKey(proId);
        int money = tcProjectFund.getMoney();
        tcProjectFund.setMoney(0);
        //插入资金变动表
        TcFundUptItem tcFundUptItem = new TcFundUptItem();
        SimpleDateFormat tempDate = new SimpleDateFormat("yyyyMMddHHmmss");
        String datetime = tempDate.format(new Date());
        tcFundUptItem.setItemId("u"+datetime);
        tcFundUptItem.setItemName("项目结余");
        tcFundUptItem.setMoney(money);
        tcFundUptItem.setOperation("1");
        tcFundUptItem.setOpTime(new Date());
        //增加公司账户余额
        IMap result1 =tcFundMapper.getFundInfo();
        int newMoney = result1.getInt("money") + money;
        TcFund tcFund =new TcFund();
        tcFund.setFundId(result1.getString("fund_id"));
        tcFund.setMoney(newMoney);
        //插入资金变动对应项目表
        TcFundProjectItem tcFundProjectItem = new TcFundProjectItem();
        tcFundProjectItem.setProId(proId);
        tcFundProjectItem.setFundItemId(tcFundUptItem.getItemId());
        try {
            tcProjectFundMapper.updateByPrimaryKeySelective(tcProjectFund);
            tcProjectMapper.updateByPrimaryKeySelective(tcProject);
            tcFundUptItemMapper.insert(tcFundUptItem);
            tcFundMapper.updateByPrimaryKeySelective(tcFund);
            tcFundProjectItemMapper.insert(tcFundProjectItem);
            opLogService.addLog(param.getString("updateUser"),"确定结束项目","项目ID："+proId);
        }catch (Exception e){
            return 0;
        }
        return 1;
    }
}
